﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook_Graph_Toolkit.FacebookObjects;
using JSON;

namespace Facebook_Graph_Toolkit.Helpers {
    /// <summary>
    /// Provides methods to manage test users
    /// </summary>
    public class TestUserManager {
        string AppAccessToken = null;
        string FacebookAppID = null;
        /// <summary>
        /// Initiate a new instance of TestUserManager
        /// </summary>
        /// <param name="AppAccessToken">App Access Token</param>
        /// <param name="FacebookAppID">App ID</param>
        public TestUserManager(string AppAccessToken, string FacebookAppID) {
            this.AppAccessToken = AppAccessToken;
            this.FacebookAppID = FacebookAppID;
        }
        /// <summary>
        /// Create a new Test User
        /// </summary>
        /// <param name="IsAppInstalled">Whether the application is installed upon user creation.</param>
        /// <param name="ExtendedPermissions">Extended Permissions granted by the user to the app</param>
        /// <returns>Details of new Test User</returns>
        public TestUser CreateTestUser(bool IsAppInstalled, string ExtendedPermissions) {
            string url = string.Format("https://graph.facebook.com/{0}/accounts/test-users",FacebookAppID);
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("installed", IsAppInstalled ? "true" : "false");
            parameters.Add("permissions", ExtendedPermissions);
            parameters.Add("method", "post");
            parameters.Add("access_token", AppAccessToken);
            url = Common.BuildUrlQuery(url, parameters);
            string TestUserString = WebResponseHelper.GetWebResponse(url);
            JsonObject JO = new JsonObject(TestUserString);
            return new TestUser(JO);
        }
        /// <summary>
        /// Gets the list of Test Users of this app. Note that emails and passwords are unavailable.
        /// </summary>
        /// <returns>List of Test Users</returns>
        public IList<TestUser> GetTestUsers() {
            IList<TestUser> U = new List<TestUser>();
            string url = string.Format("https://graph.facebook.com/{0}/accounts/test-users?access_token={1}",FacebookAppID,AppAccessToken);
            string response_string = WebResponseHelper.GetWebResponse(url);
            JsonObject JO = new JsonObject(response_string);
            JsonArray JA = (JsonArray)JO["data"];
            foreach (JsonObject TestUserJA in JA) {
                U.Add(new TestUser(TestUserJA));
            }
            return U;
        }
        /// <summary>
        /// Change the password of a Test User
        /// </summary>
        /// <param name="UserID">User ID of Test User</param>
        /// <param name="NewPassword">New Password</param>
        /// <returns>Whether the operation is successful</returns>
        public bool ChangePassword(string UserID, string NewPassword) {
            string url = string.Format("https://graph.facebook.com/{0}?password={1}&method=post&access_token={2}",UserID,NewPassword,AppAccessToken);
            string response_string = WebResponseHelper.GetWebResponse(url);
            if (response_string == "true") return true;
            return false;
        }
        /// <summary>
        /// Delete a Test User
        /// </summary>
        /// <param name="UserID">User ID of Test User</param>
        /// <returns>Whether the operation is successful</returns>
        public bool DeleteTestUser(string UserID) {
            string url = string.Format("https://graph.facebook.com/{0}?method=delete&access_token={1}", UserID, AppAccessToken);
            string response_string = WebResponseHelper.GetWebResponse(url);
            if (response_string == "true") return true;
            return false;
        }
        /// <summary>
        /// Send a friend request / accept a friend request
        /// </summary>
        /// <param name="FromUserID">UserID of Test User initiating the action</param>
        /// <param name="ToUserID">User ID of target Test User</param>
        /// <param name="FromAccessToken">Access Token of the Test User initiating the action</param>
        /// <returns>Whether the operation is successful</returns>
        public bool MakeFriends(string FromUserID, string ToUserID, string FromAccessToken){
            string url = string.Format("https://graph.facebook.com/{0}/friends/{1}?method=post&access_token={2}",FromUserID,ToUserID,FromAccessToken);
            string response_string = WebResponseHelper.GetWebResponse(url);
            if (response_string == "true") return true;
            return false;
        }
    }
}
